package cn.lovelysia.ymcc.web.controller;

import cn.lovelysia.ymcc.query.DepartmentQuery;
import cn.lovelysia.ymcc.result.JSONResult;
import cn.lovelysia.ymcc.result.PageList;
import cn.lovelysia.ymcc.service.IDepartmentService;
import cn.lovelysia.ymcc.domain.Department;
import com.baomidou.mybatisplus.plugins.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/department")
@Api(value = "部门相关接口",description = "处理部门相关请求")
public class DepartmentController {

    @Autowired
    public IDepartmentService departmentService;

    /**
    * 保存和修改公用的
    */
    @ApiOperation("添加和修改相关接口")
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody @ApiParam("保存的对象，有id则是修改，没有则是添加")
                                             @Valid Department department){
        if(department.getId()!=null){
            departmentService.updateById(department);
        }else{
            departmentService.insert(department);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @ApiOperation("删除对象相关接口")
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        departmentService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @ApiOperation("根据ID获取对象相关接口")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(departmentService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @ApiOperation("查询所有对象相关接口")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(departmentService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @ApiOperation("带条件分页查询相关接口")
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody DepartmentQuery query){
        Page<Department> page = new Page<Department>(query.getPage(),query.getRows());
        page = departmentService.selectPage(page);
        return JSONResult.success(new PageList<Department>(page.getTotal(),page.getRecords()));
    }

}
